package cq.zhcqk.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.dou361.dialogui.DialogUIUtils;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.zzhoujay.richtext.RichText;

import org.apache.http.Header;
import org.json.JSONObject;

import java.util.Map;

import cq.zhcqk.R;
import cq.zhcqk.app;
import cq.zhcqk.axst.fragment.AxstInfo;
import cq.zhcqk.base.BaseActivity;
import cq.zhcqk.util.Common;
import cq.zhcqk.util.GsonUtil;
import cq.zhcqk.util.StringUtil;
import cq.zhcqk.util.WebViewHeader;

/**
 * Created by zhangle on 2018/3/4.
 */
public class AXST_ExplainActivity extends BaseActivity implements View.OnClickListener{
    private TextView content_text;
    private RichText richText;
    private String cid="";
    private WebView webTest;
    private Button diancan;
    private Button list;
    private Button location;
    @Override
    protected void initView() {
        setContentView(R.layout.activity_axst_explain);
        content_text = (TextView) findViewById(R.id.content_text);
        webTest = (WebView)findViewById(R.id.webTest);
        diancan = (Button) findViewById(R.id.diancan);
        list = (Button) findViewById(R.id.list);
        location = (Button) findViewById(R.id.location);
        cid = getIntent().getStringExtra("cid");
        initBack();
        diancan.setOnClickListener(this);
        list.setOnClickListener(this);
        location.setOnClickListener(this);
    }

    @Override
    protected void initData() {
//        getDetail();
        setWeb();
        WebViewHeader.setHeaderAndLoad(webTest, Common.URL+"/blog/open/post/"+cid);
    }

    private void initBack(){
        ImageView back = (ImageView) findViewById(R.id.header_back);
        TextView title = (TextView) findViewById(R.id.header_text);
        title.setText("和美磁器口");
        back.setOnClickListener(this);
    }

    public void setWeb(){
        WebSettings settings = webTest.getSettings();
        settings.setJavaScriptEnabled(true);
        settings.setBuiltInZoomControls(true);
        settings.setLoadWithOverviewMode(true);
        settings.setUseWideViewPort(true);
        settings.setDatabaseEnabled(true);
        settings.setBuiltInZoomControls(false);
        settings.setSupportZoom(false);
        settings.setDisplayZoomControls(false);

        if(webTest != null)
        {
            webTest.setWebViewClient(new WebViewClient()
            {
                @Override
                public void onPageFinished(WebView view,String url)
                {
                    DialogUIUtils.dismiss(buildBean);
                }

                @Override
                public void onPageStarted(WebView view, String url, Bitmap favicon) {
                    super.onPageStarted(view, url, favicon);
                    buildBean.show();
                }
            });
        }
    }

    private void getDetail(){
        RequestParams params = new RequestParams();
        params.add("cid",cid);
        httpClient.post(Common.URL+Common.NEWS_DETAIL,params,new JsonHttpResponseHandler(){
            @Override
            public void onSuccess(int statusCode, Header[] headers,
                                  JSONObject response) {
                super.onSuccess(statusCode, headers, response);
                System.out.println("onSuccess："+response);
                Map<String, Object> map = GsonUtil.GsonToMaps(response+"");
                if(map != null && map.size()>0){
                    Map<String, Object> mapData = (Map<String, Object>) map.get("data");
                    if(mapData != null && mapData.size()>0){
                        richText = RichText.from(mapData.get("content")+"").into(content_text);
                    }
                }
            }

            @Override
            public void onFailure(int statusCode, Header[] headers,
                                  Throwable throwable, JSONObject errorResponse) {
                super.onFailure(statusCode, headers, throwable,
                        errorResponse);
                Toast.makeText(context,"获取失败，请稍后再试",Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                super.onFailure(statusCode, headers, responseString, throwable);
                Toast.makeText(context,"获取失败，请稍后再试",Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onFinish() {
                super.onFinish();
                DialogUIUtils.dismiss(buildBean);
            }
        });
    }

    private void getLocation(){
        RequestParams params = new RequestParams();
        String url = "";
        url = Common.URL+Common.LOCATION;
        buildBean.show();
        httpClient.post(url,params,new JsonHttpResponseHandler(){
            @Override
            public void onSuccess(int statusCode, Header[] headers,
                                  JSONObject response) {
                super.onSuccess(statusCode, headers, response);
                System.out.println("onSuccess："+response);
                Map<String, Object> map = GsonUtil.GsonToMaps(response+"");
                if(StringUtil.stringToInt(map.get("code")+"") != 0){
                    Toast.makeText(context,"获取地址信息失败",Toast.LENGTH_SHORT).show();
                    return;
                }
                String address = "address="+ map.get("data")+"";
                String uristr = "http://api.map.baidu.com/geocoder?" + address
                        + "&output=html&src=yhc";
                Uri uri = Uri.parse(uristr);
                Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                context.startActivity(intent);
            }

            @Override
            public void onFailure(int statusCode, Header[] headers,
                                  Throwable throwable, JSONObject errorResponse) {
                super.onFailure(statusCode, headers, throwable,
                        errorResponse);
                System.out.println("errorResponse："+errorResponse);
                Toast.makeText(context,"获取失败，请稍后再试",Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                super.onFailure(statusCode, headers, responseString, throwable);
                System.out.println("responseString："+responseString);
                Toast.makeText(context,"获取失败，请稍后再试",Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onFinish() {
                super.onFinish();
                DialogUIUtils.dismiss(buildBean);
            }
        });
    }

    @Override
    public void onClick(View v) {
        Intent intent = null;
        switch (v.getId()){
            case R.id.header_back:
                finish();
                break;
            case R.id.diancan:
                intent = new Intent(context,AXST_WebActivity.class);
//                intent = new Intent(context,AxstInfo.class);
                startActivity(intent);
                overridePendingTransition(R.anim.in_from_right, R.anim.out_to_left);
                break;
            case R.id.list:
                if(!app.isLogin){
                    Toast.makeText(context,"请先登录",Toast.LENGTH_SHORT).show();
                    context.startActivity(new Intent(context, LoginActivity.class));
                    overridePendingTransition(R.anim.in_from_right, R.anim.out_to_left);
                    return;
                }
                startActivity(new Intent(this,AXST_OrderListActivity.class));
                overridePendingTransition(R.anim.in_from_right, R.anim.out_to_left);
                break;
            case R.id.location:
                getLocation();
                break;
        }
    }
}
